<template>

  <!-- 自定义对话框 -->
  <custom-dialog 
    ref="customDialogRef" 
    v-slot="{ formData }" 
    :api-map="{ update: updateSupervision }" 
    header="转派监理"
    @request-success="handleDialogRequestSuccess">
    <t-form-item label="监理" name="supervisionId" :rules="[{ required: true, message: '请选择监理' }]">
      <t-select 
        v-model:value="formData.supervisionId" 
        :options="supervisionList"
        :keys="{ label: 'nickname', value: 'id' }" 
        placeholder="请选择监理"
        clearable 
        filterable 
        @clear="formData.supervisionId = null" />
    </t-form-item>
  </custom-dialog>
</template>

<script setup lang="ts">
import { MessagePlugin } from 'tdesign-vue-next';
import { PropType, ref, useTemplateRef } from 'vue';

import { updateSupervision, getSupervisionList } from '@/api/project/engineering';
import CustomDialog from '@/components/custom/dialog/index.vue';


// props
const props = defineProps({
  // 施工id
  id: {
    type: Number,
    required: true,
  },
});

// emits
const emits = defineEmits(['requestSuccess']);

// 监理
const supervisionList = ref([]);
// 处理获取监理
const handleGetsupervisionList = (engineeringManagerId: any) => {
  const params = {
    engineeringManagerId
  };
  getSupervisionList(params)
    .then((data) => {
      supervisionList.value = data;
    })
    .catch(({ message }) => {
      MessagePlugin.error(message);
    });
};

// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  handleGetsupervisionList(params.engineeringManagerId);
  const engineeringId = params.engineeringId || params.id
  customDialogRef.value.open(mode, { id: engineeringId, supervisionId: params.supervisionId });
};
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  emits('requestSuccess', result);
};

defineExpose({
  handleOpenDialog,
});
</script>
